package com.pro.addresslist.ui.view;


import java.sql.Blob;
import java.util.List;

import com.pro.addresslist.R;
import com.pro.addresslist.ui.adapter.CommonAdapter;
import com.pro.addresslist.ui.adapter.ViewHolder;

import android.content.Context;
import android.graphics.drawable.BitmapDrawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.PopupWindow;

public class PopMenu{
	private Context context;
	private PopupWindow popupWindow ;
	private View view;
	private ListView listView;
	private CommonAdapter<Object[]> adp;
	

	public PopMenu(Context context,int layout,int listview,int list_itme_layout,List<Object[]> listitm,final int textid,final int imgid,int width) {
		// TODO Auto-generated constructor stub
		this.context = context;
		view = LayoutInflater.from(context).inflate(layout, null);
		//设置 listview  
        listView = (ListView)view.findViewById(listview);
        adp=new CommonAdapter<Object[]>(context,listitm,list_itme_layout) {
			
			@Override
			public void convert(ViewHolder holder, Object[] t) {
				if(textid!=0){
					holder.setText(textid, t[0].toString());
				}
				if(imgid!=0){
					holder.setImageResource(imgid, Integer.parseInt(t[1].toString()));
				}
			}
		};
        listView.setAdapter(adp);
        
        popupWindow = new PopupWindow(view,width,LayoutParams.WRAP_CONTENT);
        
        // 这个是为了点击“返回Back”也能使其消失，并且并不会影响你的背景（很神奇的）
        popupWindow.setBackgroundDrawable(new BitmapDrawable());
	}

	
	//下拉式 弹出 pop菜单 parent 右下角
	public void showAsDropDown(View parent,int xoff,int yoff,Boolean Outside) {
		popupWindow.showAsDropDown(parent, xoff, yoff);
		
		// 使其聚集
        popupWindow.setFocusable(Outside);
        // 设置允许在外点击消失
        popupWindow.setOutsideTouchable(Outside);
        //刷新状态
        popupWindow.update();
	}
	
	//隐藏菜单
	public void dismiss() {
		popupWindow.dismiss();
	}

	//设置菜单项点击监听器  
    public void setOnItemClickListener(OnItemClickListener listener) {  
        //this.listener = listener;  
        listView.setOnItemClickListener(listener);  
    }  
}

